/*
 * Copyright (C) 2010 Moduad Co., Ltd.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.androidpn.server.xmpp.handler;

import org.androidpn.server.entity.RoomMessage;
import org.androidpn.server.service.RoleService;
import org.androidpn.server.service.RoomMessageService;
import org.androidpn.server.service.RoomService;
import org.androidpn.server.service.ServiceLocator;
import org.androidpn.server.xmpp.UnauthorizedException;
import org.androidpn.server.xmpp.packet.RoomMessagePacket.Operation;
import org.androidpn.server.xmpp.push.PushRoomMessageToClientThread;
import org.androidpn.server.xmpp.session.ClientSession;
import org.dom4j.Element;
import org.jivesoftware.smack.util.StringUtils;
import org.xmpp.packet.IQ;
import org.xmpp.packet.PacketError;

/**
 * This class is to handle the TYPE_IQ jabber:iq:roster protocol.
 * 
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public class IQRoomMessageHandler extends IQHandler {

	private static final String NAMESPACE = "jabber:iq:roomMessage";

	private RoleService roleService;

	private RoomService roomService;

	private RoomMessageService roomMessageService;

	/**
	 * Constructor.
	 */
	public IQRoomMessageHandler() {
		roleService = (RoleService) ServiceLocator
				.getService(ServiceLocator.ROLE_SERVICE);
		roomService = (RoomService) ServiceLocator
				.getService(ServiceLocator.ROOM_SERVICE);
		roomMessageService = (RoomMessageService) ServiceLocator
				.getService(ServiceLocator.ROOM_MESSAGE_SERVICE);
	}

	/**
	 * Handles the received IQ packet.
	 * 
	 * @param packet
	 *            the packet
	 * @return the response to send back
	 * @throws UnauthorizedException
	 *             if the user is not authorized
	 */
	public IQ handleIQ(IQ packet) throws UnauthorizedException {
		IQ reply = null;

		ClientSession session = sessionManager.getSession(packet.getFrom());

		try {
			Element query = packet.getChildElement();
			reply = IQ.createResultIQ(packet);
			String operationStr = query.elementText("operation");
			if (operationStr != null && !operationStr.equals("")) {
				Operation operation = Operation.valueOf(operationStr);
				switch (operation) {
				case getMessage:
					break;
				case pushMessage:
					break;
				case sendMessage:
					String roomMessageXML = StringUtils.unescapeNode(query
							.elementText("roomMessage"));
					RoomMessage roomMessage = RoomMessage
							.fromXML(roomMessageXML);
					roomMessage.setId(null);
					roomMessage.setRole(roleService.find(roomMessage.getRole()
							.getId()));
					roomMessage.setRoom(roomService.find(roomMessage.getRoom()
							.getId()));
//					// 保存
//					roomMessageService.save(roomMessage);
					// 发送房间消息线程启动
					new Thread(new PushRoomMessageToClientThread(roomMessage,
							sessionManager)).start();
					break;
				default:
					reply.setError(PacketError.Condition.feature_not_implemented);
					break;
				}
			} else {
				reply = null;
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			reply.setError(PacketError.Condition.feature_not_implemented);
		}
		if (reply != null) {
			session.process(reply);
		}
		return null;
	}

	public String getNamespace() {
		return NAMESPACE;
	}
}
